package cn.aitrox.ry.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZipUtil {
    private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);

    public ZipUtil() {
    }

    public static boolean zipPath(String sourceFilePath, File zipFile) {
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        List<BufferedInputStream> bisList = new ArrayList();
        if (!sourceFile.exists()) {
            log.debug("待压缩的文件目录：" + sourceFilePath + "不存在.");
        } else {
            boolean var25 = false;

            try {
                var25 = true;
                FsUtil.ensureDirExist(zipFile);
                if (zipFile.exists()) {
                    zipFile.delete();
                }

                File[] sourceFiles = sourceFile.listFiles();
                if (null != sourceFiles && sourceFiles.length >= 1) {
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    byte[] bufs = new byte[10240];
                    int i = 0;

                    while(true) {
                        if (i >= sourceFiles.length) {
                            flag = true;
                            var25 = false;
                            break;
                        }

                        ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                        zos.putNextEntry(zipEntry);
                        fis = new FileInputStream(sourceFiles[i]);
                        BufferedInputStream bis = new BufferedInputStream(fis, 10240);
                        bisList.add(bis);
                        boolean var13 = false;

                        int read;
                        while((read = bis.read(bufs, 0, 10240)) != -1) {
                            zos.write(bufs, 0, read);
                        }

                        ++i;
                    }
                } else {
                    log.debug("待压缩的文件目录：" + sourceFilePath + "里面不存在文件，无需压缩.");
                    var25 = false;
                }
            } catch (FileNotFoundException var30) {
                throw new RuntimeException(var30);
            } catch (IOException var31) {
                throw new RuntimeException(var31);
            } finally {
                if (var25) {
                    if (!ArrayUtil.isEmpty(bisList)) {
                        Iterator var15 = bisList.iterator();

                        while(var15.hasNext()) {
                            BufferedInputStream bis = (BufferedInputStream)var15.next();

                            try {
                                bis.close();
                            } catch (IOException var27) {
                                log.error("zip close bis error", var27);
                            }
                        }
                    }

                    if (null != zos) {
                        try {
                            zos.close();
                        } catch (IOException var26) {
                            log.error("zip close zos error", var26);
                        }
                    }

                }
            }

            if (!ArrayUtil.isEmpty(bisList)) {
                Iterator var33 = bisList.iterator();

                while(var33.hasNext()) {
                    BufferedInputStream bis = (BufferedInputStream)var33.next();

                    try {
                        bis.close();
                    } catch (IOException var29) {
                        log.error("zip close bis error", var29);
                    }
                }
            }

            if (null != zos) {
                try {
                    zos.close();
                } catch (IOException var28) {
                    log.error("zip close zos error", var28);
                }
            }
        }

        return flag;
    }

    public static void zip(String sourceFilePath, String zipFilePath) {
        File zipFile = new File(zipFilePath);
        File sourceFile = new File(sourceFilePath);
        FsUtil.ensureDirExist(zipFile);
        if (zipFile.exists()) {
            zipFile.delete();
        }

        if (!sourceFile.exists()) {
            log.debug("待压缩的文件目录：" + sourceFilePath + "不存在.");
        } else {
            try {
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilePath));
                Throwable var5 = null;

                try {
                    BufferedOutputStream bos = new BufferedOutputStream(out);
                    Throwable var7 = null;

                    try {
                        compress(out, bos, sourceFile, sourceFile.getName());
                    } catch (Throwable var32) {
                        var7 = var32;
                        throw var32;
                    } finally {
                        if (bos != null) {
                            if (var7 != null) {
                                try {
                                    bos.close();
                                } catch (Throwable var31) {
                                    var7.addSuppressed(var31);
                                }
                            } else {
                                bos.close();
                            }
                        }

                    }
                } catch (Throwable var34) {
                    var5 = var34;
                    throw var34;
                } finally {
                    if (out != null) {
                        if (var5 != null) {
                            try {
                                out.close();
                            } catch (Throwable var30) {
                                var5.addSuppressed(var30);
                            }
                        } else {
                            out.close();
                        }
                    }

                }
            } catch (Exception var36) {
                log.error("zip", var36);
            }
        }

    }

    public static void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception {
        if (sourceFile.isDirectory()) {
            File[] flist = sourceFile.listFiles();
            if (flist.length == 0) {
                out.putNextEntry(new ZipEntry(base + "/"));
            } else {
                for(int i = 0; i < flist.length; ++i) {
                    compress(out, bos, flist[i], base + "/" + flist[i].getName());
                }
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);

            int tag;
            while((tag = bis.read()) != -1) {
                out.write(tag);
            }

            bis.close();
            fos.close();
        }

    }
}